//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows.Media;
using OpenLS.Drawing;
using OpenLS.Drawing.UI;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class TableBackgroundControl: TableControl
    {





        protected override void OnRender(DrawingContext drawingContext)
        {
            var table = (Table) DataContext;
            if (table == null)
                return;
            TableStyle style = table.Style;
            if (style == null)
                return;
            ColorScheme colorScheme = DrawingElement.GetColorScheme(this);
            Worksheet worksheet = SpreadsheetElement.GetWorksheet(this);
            int minRow = Top;
            int minColumn = Left;
            int maxRow = minRow;
            {
                double s = 0;
                while (s < ActualHeight)
                {
                    Row row = worksheet.Rows[maxRow];
                    if (row.IsVisible)
                        s += row.ActualHeight;
                    maxRow++;
                }
            }
            int maxColumn = minColumn;
            {
                double s = 0;
                while (s < ActualWidth)
                {
                    Column column = worksheet.Columns[maxColumn];
                    if (column.IsVisible)
                        s += column.ActualWidth;
                    maxColumn++;
                }
            }
            int totalCount;
            totalCount = table.TotalsRowShown ? table.TotalsRowCount : 0;
            DrawWholeTable(drawingContext, style, worksheet, minRow, minColumn, maxRow, maxColumn, colorScheme);
            if (table.ShowColumnStripes)
                DrawColumns(drawingContext, style, worksheet, minRow, minColumn, maxRow, maxColumn, colorScheme,
                            totalCount);
            if (table.ShowRowStripes)
                DrawRows(drawingContext, style, worksheet, minRow + table.HeaderRowCount, minColumn, maxRow - totalCount, maxColumn, colorScheme);
            if (table.ShowFirstColumn)
                DrawFirstColumn(drawingContext, style, worksheet, minRow, minColumn, maxRow, colorScheme);
            if (table.ShowLastColumn)
                DrawLastColumn(drawingContext, style, worksheet, minRow, maxRow,
                               table.Range.Column + table.Range.ColumnSpan - 1, colorScheme);
            if (totalCount > 0)
                DrawTotalRows(drawingContext, style, worksheet, minColumn, maxRow, maxColumn, colorScheme,
                              table.Range.Row + table.Range.RowSpan, totalCount);
            if (table.HeaderRowCount > 0)
                DrawHeaderRow(drawingContext, style, worksheet, minColumn, maxRow, maxColumn, colorScheme, table.Range.Row);
            DrawCell(drawingContext, worksheet, table.Range.Row, table.Range.Column, style.FirstHeaderCell, colorScheme);
            DrawCell(drawingContext, worksheet, table.Range.Row + table.Range.RowSpan - 1, table.Range.Column,
                     style.FirstTotalCell, colorScheme);
            DrawCell(drawingContext, worksheet, table.Range.Row, table.Range.Column + table.Range.ColumnSpan - 1,
                     style.LastHeaderCell, colorScheme);
            DrawCell(drawingContext, worksheet, table.Range.Row + table.Range.RowSpan - 1,
                     table.Range.Column + table.Range.ColumnSpan - 1, style.LastTotalCell, colorScheme);
        }

        private void DrawTotalRows(DrawingContext drawingContext, TableStyle style, Worksheet worksheet, int minColumn, int maxRow, int maxColumn, ColorScheme colorScheme, int afterRow, int totalRowCount)
        {
            if (totalRowCount == 0)
                return;
            TableStyleElement element = style.TotalRow;
            Brush brush = GetBrush(element, colorScheme);
            if (brush == null)
                return;
            drawingContext.DrawRectangle(brush, null, GetRect(afterRow - totalRowCount, minColumn, afterRow, maxColumn, worksheet));
        }
        private void DrawHeaderRow(DrawingContext drawingContext, TableStyle style, Worksheet worksheet, int minColumn, int maxRow, int maxColumn, ColorScheme colorScheme, int headerRow)
        {
            TableStyleElement element = style.HeaderRow;
            Brush brush = GetBrush(element, colorScheme);
            if (brush == null)
                return;
            drawingContext.DrawRectangle(brush, null, GetRect(headerRow, minColumn, headerRow + 1, maxColumn, worksheet));
        }
        private void DrawCell(DrawingContext drawingContext, Worksheet worksheet, int row, int column, TableStyleElement type, ColorScheme colorScheme)
        {
            TableStyleElement element = type;
            Brush brush = GetBrush(element, colorScheme);
            if (brush == null)
                return;
            drawingContext.DrawRectangle(brush, null, GetRect(row, column, row+ 1, column+1, worksheet));
        }

        private void DrawLastColumn(DrawingContext drawingContext, TableStyle style, Worksheet worksheet, int minRow, int maxRow, int column, ColorScheme colorScheme)
        {
            TableStyleElement element = style.LastColumn;
            Brush brush = GetBrush(element, colorScheme);
            if (brush == null)
                return;
            drawingContext.DrawRectangle(brush, null, GetRect(minRow, column, maxRow, column + 1, worksheet));
        }

        private void DrawWholeTable(DrawingContext drawingContext, TableStyle style, Worksheet worksheet, int minRow, int minColumn, int maxRow, int maxColumn, ColorScheme colorScheme)
        {
            TableStyleElement element = style.WholeTable;
            if (element != null)
            {
                if (element.StyleRecord.Background != null)
                {
                    drawingContext.DrawRectangle(GetBrush(element, colorScheme),
                                                 null,
                                                 GetRect(minRow, minColumn, maxRow, maxColumn, worksheet));
                }
            }
        }

        private static Brush GetBrush(TableStyleElement element, ColorScheme colorScheme)
        {
            if (element == null)
                return null;
            if (element.StyleRecord == null)
                return null;
            return FillToBrushConverter.GetBrush(element.StyleRecord.Background, colorScheme);
        }

        private void DrawRows(DrawingContext drawingContext, TableStyle style, Worksheet worksheet, int minRow, int minColumn, int maxRow, int maxColumn, ColorScheme colorScheme)
        {
            TableStyleElement firstRowStripe = style.FirstRowStripe;
            TableStyleElement secondRowStripe = style.SecondRowStripe;
            if (firstRowStripe == null && secondRowStripe == null)
                return;
            Brush firstRowBrush = GetBrush(firstRowStripe, colorScheme);
            Brush secondRowBrush = GetBrush(secondRowStripe, colorScheme);
            int firstRowSpan = firstRowStripe != null ? firstRowStripe.Size : 1;
            int secondRowSpan = secondRowStripe != null ? secondRowStripe.Size : 1;
            for (int row = minRow; row < maxRow; )
            {
                if (firstRowBrush != null)
                    drawingContext.DrawRectangle(firstRowBrush, null, GetRect(row, minColumn, Math.Min(row + firstRowSpan, maxRow), maxColumn, worksheet));
                row += firstRowSpan;
                if (secondRowBrush != null)
                    drawingContext.DrawRectangle(secondRowBrush, null, GetRect(row, minColumn, Math.Min(row + secondRowSpan, maxRow), maxColumn, worksheet));
                row += secondRowSpan;
            }
        }
        private void DrawColumns(DrawingContext drawingContext, TableStyle style, Worksheet worksheet, int minRow, int minColumn, int maxRow, int maxColumn, ColorScheme colorScheme, int totalRowCount)
        {
            minRow++;
            maxRow -= totalRowCount;
            if (minRow > maxRow)
                return;
            TableStyleElement firstColumnStripe = style.FirstColumnStripe;
            TableStyleElement secondColumnStripe = style.SecondColumnStripe;
            if (firstColumnStripe == null && secondColumnStripe == null)
                return;
            Brush firstColumnBrush = GetBrush(firstColumnStripe, colorScheme);
            Brush secondColumnBrush = GetBrush(secondColumnStripe, colorScheme);
            int firstColumnSpan = firstColumnStripe != null ? firstColumnStripe.Size : 1;
            int secondColumnSpan = secondColumnStripe != null ? secondColumnStripe.Size : 1;
            for (int column = minColumn; column < maxColumn; )
            {
                if (firstColumnBrush != null)
                    drawingContext.DrawRectangle(firstColumnBrush, null, GetRect(minRow , column, maxRow, Math.Min(column + firstColumnSpan, maxColumn), worksheet));
                column += firstColumnSpan;
                if (secondColumnBrush != null)
                    drawingContext.DrawRectangle(secondColumnBrush, null, GetRect(minRow, column, maxRow, Math.Min(column + secondColumnSpan, maxColumn), worksheet));
                column += secondColumnSpan;
            }
        }

        private void DrawFirstColumn(DrawingContext drawingContext, TableStyle style, Worksheet worksheet, int minRow, int minColumn, int maxRow, ColorScheme colorScheme)
        {
            TableStyleElement firstColumn = style.FirstColumn;
            Brush firstColumnBrush = GetBrush(firstColumn, colorScheme);
            if (firstColumnBrush == null)
                return;
            drawingContext.DrawRectangle(firstColumnBrush, null, GetRect(minRow, minColumn, maxRow, minColumn + 1, worksheet));
        }
    }
}